package com.btl.service.ai.support.rag.generation.knowledge;

import com.btl.service.ai.support.rag.generation.GenerationModelEnum;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Configuration
public class GenerationKnowledgeFactory {

    @Resource
    private ApplicationContext applicationContext;

    private static final Map<GenerationModelEnum, GenerationKnowledgeStrategy> strategyMap = new ConcurrentHashMap<>();


    @PostConstruct
    void init() {
        Collection<GenerationKnowledgeStrategy> values = applicationContext.getBeansOfType(GenerationKnowledgeStrategy.class)
                .values();
        for (GenerationKnowledgeStrategy value : values) {
            strategyMap.put(value.model(), value);
        }
    }

    public static GenerationKnowledgeStrategy getStrategy(GenerationModelEnum generationModelEnum) {
        return strategyMap.getOrDefault(generationModelEnum, strategyMap.get(GenerationModelEnum.OPEN_AI_CHAT_MODEL));
    }

}
